package client

import (
	"bsinpass/go/mpc/common"

	"github.com/ethereum/go-ethereum/ethclient"

	sdk "github.com/Conflux-Chain/go-conflux-sdk"
)

var txConf = &common.GlobalConf
var txLog = common.GetNftLog("client")

func GetEthClient(chain string) (*ethclient.Client, common.NftError) {
	if !common.GlobalConf.IsLoaded {
		common.LoadConfig("../../conf/config.conf")
	}
	v, ok := txConf.SupportChains[chain]
	if !ok {
		txLog.LogE("Unsupported input chain [%v]", chain)
		return nil, common.NewError(505)
	}
	client, err := ethclient.Dial(v.NodeUrl)
	if err != nil {
		txLog.LogE("Get eth client failed! [%v]", err)
		return nil, common.NewError(500)
	}
	return client, common.NewError(200)
}

func GetCfxClient(chain string) (*sdk.Client, common.NftError) {
	if !common.GlobalConf.IsLoaded {
		common.LoadConfig("../../conf/config.conf")
	}
	_, ok := txConf.SupportChains[chain]
	if !ok {
		txLog.LogE("Unsupported input chain [%v]", chain)
		return nil, common.NewError(505)
	}

	if common.GlobalConf.CfxClient == nil {
		txLog.LogE("create cfx client failed [%v]", chain)
		return nil, common.NewError(508)
	}
	client := common.GlobalConf.CfxClient
	return client, common.NewError(200)
}
